﻿using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{
    /// <summary>
    /// 三工位设备  横抓  夹爪离心管
    /// </summary>
    public class DevThreeStationAcrossClaw : DevBase



    {
        public DevThreeStationAcrossClaw(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }


        /// <summary>
        /// 夹爪开合
        /// </summary>
        /// <param name="journey"></param>
        /// <returns></returns>
        public bool OpenOrCloseClaw(float journey)
        {

            bool temperature = false;
            if (journey > 20 || journey < 0)
            {
                return temperature;
            }
            byte[] array1 = BitConverter.GetBytes(journey);
            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //寄存器地址
                ushort startAddr = 0x08;
                //发送数据
                byte[] array2 = new byte[] {
                    0x04,0x02,0x00,0xEC
                };
                byte[] floatBytes = new byte[array1.Length + array2.Length];
                Array.Copy(array1, 0, floatBytes, 0, array1.Length);
                Array.Copy(array2, 0, floatBytes, array1.Length, array2.Length);

                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }

        /// <summary>
        /// 打开夹爪
        /// </summary>
        /// <returns></returns>
        public bool OpenClaw()
        {

            bool temperature = false;
           // 01 10 08 EC 00 02 04 00 00 40 C0 AB E2
            try
            {
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x10,0x08,0xEC,0x00,0x02,0x04,0x00,0x00,0x40,0xC0,0xAB,0xE2
                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }

        /// <summary>
        /// 关闭夹爪
        /// </summary>
        /// <param name="journey"></param>
        /// <returns></returns>
        public bool CloseClaw()
        {

            bool temperature = false;

            try
            {
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x10,0x08,0xEC,0x00,0x02,0x04,0x00,0x00,0x41,0xA0,0xAA,0x5A
                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }
    }
}
